package com.nightwalkbuddy.dao;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.nightwalkbuddy.dto.MapPair;
import com.nightwalkbuddy.dto.MapPath;

/**
 * SQLite implementation for saving path in db
 * 
 * @author htisabel
 * 
 */
public class PathProviderLive extends SQLiteOpenHelper implements IPathDAO {

	// collection of long/lat pair for 15 UC campus building locations
	ArrayList<MapPair<Double, Double>> ucLocations = new ArrayList<MapPair<Double, Double>>();
	MapPath aMapPath = new MapPath();
	// SQLite db
	private static final String DB_NAME = "nightwalkPath.db";
	private static final String TABLE_NAME = "nightwalkPath";
	private static final int DB_VERSION = 1;
	private static final String _ID = "pathID", USERNAME = "username",
			MAPPATH = "mapPath";
	private static final String CREATE_DB = "CREATE TABLE " + TABLE_NAME + "("
			+ _ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + USERNAME
			+ " TEXT NOT NULL," + MAPPATH + " TEXT NOT NULL);";
	private static final String UPGRADE_MSG = "DB upgraded, old data destroyed. New version: ";
	private static final String FETCH_ERROR = "path not found in db";

	/**
	 * constructor
	 * 
	 * @param context
	 *            the context where PathProviderLive is initiated. Used for
	 *            SQLiteOpenHelper super class
	 */
	public PathProviderLive(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		this.setUCLocations();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_DB);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(PathProviderLive.class.getName(), UPGRADE_MSG + newVersion);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}

	@Override
	public void setUCLocations() {
		ucLocations.add(0, new MapPair<Double, Double>(39.130805, -84.512523));
		ucLocations.add(1, new MapPair<Double, Double>(39.131026, -84.513473));
		ucLocations.add(2, new MapPair<Double, Double>(39.131113, -84.514787));
		ucLocations.add(3, new MapPair<Double, Double>(39.129211, -84.512459));
		ucLocations.add(4, new MapPair<Double, Double>(39.128899, -84.513773));
		ucLocations.add(5, new MapPair<Double, Double>(39.128516, -84.5152));
		ucLocations.add(6, new MapPair<Double, Double>(39.130048, -84.51711));
		ucLocations.add(7, new MapPair<Double, Double>(39.131433, -84.516691));
		ucLocations.add(8, new MapPair<Double, Double>(39.130739, -84.515838));
		ucLocations.add(9, new MapPair<Double, Double>(39.131696, -84.51704));
		ucLocations.add(10, new MapPair<Double, Double>(39.131779, -84.517721));
		ucLocations.add(11, new MapPair<Double, Double>(39.132187, -84.51557));
		ucLocations.add(12, new MapPair<Double, Double>(39.130347, -84.518703));
		ucLocations.add(13, new MapPair<Double, Double>(39.130168, -84.519642));
		ucLocations.add(14, new MapPair<Double, Double>(39.13098, -84.518531));
	}

	// get the long/lat info for the building user specifies as destination
	@Override
	public MapPair<Double, Double> getUCLocation(int id) {
		return ucLocations.get(id);
	}

	@Override
	public void save(String username, String path) throws Exception {

		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(USERNAME, username); // username
		values.put(MAPPATH, path); // path
		try {
			db.insert(TABLE_NAME, null, values);
		} catch (SQLiteException e) {
			Log.w(TABLE_NAME, e.toString());
		}

		db.close();
	}

	@Override
	public MapPath fetch(int id, String pathName) throws Exception {
		SQLiteDatabase db = this.getReadableDatabase();
		MapPath mp = new MapPath();
		try {
			Cursor cursor = db.query(TABLE_NAME, new String[] { USERNAME,
					MAPPATH }, _ID + "=?", new String[] { String.valueOf(id) },
					null, null, null);
			if (cursor != null) {
				cursor.moveToFirst();
				mp.setMapPrint(cursor.getString(2));
			} else {
				Log.e(TABLE_NAME, FETCH_ERROR);
			}
		} catch (SQLiteException e) {
			Log.w(TABLE_NAME, e.toString());
		}
		db.close();
		return mp;
	}

}
